package com.zh.ssmshirofreemarker.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zh.ssmshirofreemarker.Utils.ResponseUtil;
import com.zh.ssmshirofreemarker.entity.DataTable;
import com.zh.ssmshirofreemarker.entity.User;
import com.zh.ssmshirofreemarker.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.ws.rs.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZH
 * @since 2018-07-24
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @RequiresPermissions("user:view")
    @RequestMapping(method = RequestMethod.GET)
    public String user(){
        return "user/init";
    }

    @RequiresPermissions("user:view")
    @ResponseBody
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public DataTable getAllUser(Page<User> page, User user,Integer draw){
        List<User> list = null;
        try{
            list  = userService.selectUserPage(page,user).getRecords();
        } catch (Exception e){
            e.printStackTrace();
        }
        DataTable dataTable = new DataTable(list.toArray());
        dataTable.setDraw(String.valueOf(draw));
        dataTable.setRecordsFiltered(String.valueOf(page.getTotal()));
        dataTable.setRecordsTotal(String.valueOf(page.getTotal()));
        return dataTable;
    }

    @RequiresPermissions("user:add")
    @RequestMapping("/add")
    public String add(){
       return "user/add-edit";
    }

    @RequiresPermissions("user:add")
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public ResponseUtil doAdd(User user){
       return userService.addUser(user);
    }

    @RequiresPermissions("user:update")
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseUtil updateUser(User user){
        return ResponseUtil.success(userService.updateById(user));
    }

    @RequiresPermissions("user:delete")
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseUtil deleteUser(String ids){
        return ResponseUtil.success(userService.deleteUsersByIds(ids));
    }

    @ResponseBody
    @RequestMapping(value = "/open",method = RequestMethod.PUT)
    public ResponseUtil openUser(String ids){
        return ResponseUtil.success(userService.openUsersByIds(ids));
    }

    @RequestMapping("/bindRole/{id}")
    public String bindRole(@PathVariable("id") Long id,Map<String,Object> map){
        List<Map<String,Object>> roleList = null;
        try{
            roleList = userService.getAllRoleAndSignHasRole(id);
        } catch(Exception e){
            e.printStackTrace();
        }
        System.out.println(roleList);
        map.put("roleList",roleList);
        return "user/bindRole";
    }

    @ResponseBody
    @RequestMapping("check")
    public String checkUserName(User user){
        return String.valueOf(userService.checkUserName(user));
    }

}

